<?php
/**
* 
*/
class CoursesModel extends MY_Model
{
	
	public function __construct()
	{
		parent::__construct();
		$this->table = 'nmp_course';
	}


	//获取课程数据
	public function get_courses_list($term='',$page=1,$size=50)
	{
		//查询搜索
		$where = ' where c.status = 1 ';
		if (!empty($term['venue'])) {
			$where .= 'AND c.shop_id ='.$term['venue'];
		}

		if (isset($term['keyword']) && !empty($term['keyword'])) {

			$where .= " AND c.name LIKE '%".$term['keyword']."%'";
		}

		if (!empty($term['coach'])) {
			$where .= ' AND c.cid ='.$term['coach'];
		}

		$query = $this->db->query("SELECT c.id, c.name, c.title, c.theme_img, c.nandu, c.duration, s.name as venue, co.nickname as coach FROM ".$this->table." as c LEFT JOIN nmp_shop as s ON c.shop_id = s.id JOIN nmp_coach as co ON c.cid = co.id".$where." ORDER BY c.id DESC LIMIT ".($page-1)*$size.",".$size);

		$data = $query->result_array();

		$query = $this->db->query("select COUNT(*)  as num from ".$this->table." as c".$where);
		$row = $query->row_array();

		//总数
		$count = $row['num'];
		//总页数
		$pages = ceil($count/$size);
		
		return array('list'=>$data,'count'=>$count, 'pages'=>$pages);
	}


	//获取单行数据
	public function get_courses_row($courses_id)
	{
		$this->db->select("* ");
		$this->db->from($this->table);
		$this->db->where('id', $courses_id);

		$query = $this->db->get();

		return $query->row_array();

	}


	//添加
	public function inser_course_data($arr=array())
	{
		$arr['addtime'] = time();
		if ($this->db->insert($this->table,$arr)) 
		{
			return true;
		}
		else
		{
			return false;
		}

	}

	
	//更新数据
	public function updata_course_data($arr =array(),$coach_id ='')
	{
		if ($this->db->update($this->table,$arr,array('id'=>$coach_id))){			
			return true;
		}else{
			return false;
		}

	}


	//删除
	public function remove_course($course_id)
	{
		return $this->db->delete($this->table, array('id'=>$course_id));
	}

	function is_have_appoint($cid){
		$date = date('Y-m-d');
		$sql = "select count(*) as num from nmp_user_appoint where cid='{$cid}' AND status =0 AND sdate >='{$date}' ";
		$query = $this->db->query($sql);
		$row = $query->row_array();
		
		return $row?$row['num']:0;
	}


}





?>